import { useEffect, useState } from "react"
import store from "../../store"
import actionCreator from "../../store/actions/counter"
let num = store.getState()
const Counter = () => {
    let [count, setCount] = useState(num)
    // useEffect(() => {
    //     fetch("/api/data/index.json")
    //         .then(body=>body.json())
    //         .then(res => {
    //             console.log(res)
    //         })

    //     return () => {

    //     }
    // })



    //自增

    let increment = () => {
        //派发action 变更state
        store.dispatch(actionCreator("INCREASE_COUNT", 5))
    }
    //自减

    let decrement = () => {
        store.dispatch(actionCreator("DECREASE_COUNT", 5))
    }

    //订阅store的变化

    // store.subscribe(() => {
    //     // console.log(store.getState())
    //     setCount(store.getState())
    // })

    return (
        <fieldset>
            <legend>计数器</legend>
            <button onClick={decrement}>-</button>
            &nbsp;
            <b>{store.getState()}</b>
            &nbsp;
            <button onClick={increment}>+</button>
        </fieldset>
    )
}

export default Counter